package com.hyzx.qbasic.admin.domain.repository.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.po.CmsChannelInfoPO;
import com.hyzx.qbasic.admin.model.po.CmsChannelPO;
import com.hyzx.qbasic.admin.domain.dao.CmsChannelMapper;
import com.hyzx.qbasic.admin.domain.repository.ICmsChannelRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by 黄江华 on 2018/7/24
 */

@Repository
public class CmsChannelRepositoryImpl implements ICmsChannelRepository {

    @Autowired
    private CmsChannelMapper channelMapper;

    @Override
    public List<CmsChannelPO> getAllCmsChannelList() {
        return channelMapper.getAllCmsChannelList();
    }

    @Override
    public PageInfo<CmsChannelInfoPO> getPagedCmsChannelList(int pageNum, int pageSize, String channelName, Integer channelId) {
        PageInfo pageInfo = PageHelper.startPage(pageNum, pageSize)
                .doSelectPageInfo(() -> channelMapper.getPagedCmsChannelList(channelName, channelId));

        return pageInfo;
    }

    @Override
    public boolean addCmsChannel(CmsChannelPO channel) {
        return channelMapper.addCmsChannel(channel) > 0;
    }

    @Override
    @Transactional
    public boolean deleteCmsChannel(List<Integer> channelIdList) {
        //级联删除栏目及其子栏目
        int deleteCmsChannelResult = channelMapper.deleteCmsChannel(channelIdList);
        channelMapper.deleteChannelContents(channelIdList);

        return deleteCmsChannelResult > 0;
    }

    @Override
    public CmsChannelPO getCmsChannelInfo(int channelId) {
        return channelMapper.getCmsChannelInfo(channelId);
    }

    @Override
    public boolean editCmsChannel(CmsChannelPO channel) {
        return channelMapper.editCmsChannel(channel) > 0;
    }
}
